<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
    <style>
        .comment {
            padding-left: 15px;
            width: 80%;
            margin: auto;
            margin-top: 50px;
        }
    </style>
</head>

<body>

    <template id="comment">
        <div class="comment">
            <div class="form-group">
                <label>评论人：</label>
                <input type="text" v-model:value="name" class="form-control">
            </div>

            <div class="form-group">
                <label>评论内容：</label>
                <textarea v-model:value="comment" class="form-control" rows="5"></textarea>
            </div>

            <button type="button" class="btn btn-success" @click="add">发表评论</button>

        </div>
    </template>


    <div id="app">
        <comment @com="myadd"></comment>
        <ul class="list-group comment">
            <li class="list-group-item" v-for="item in list" :key="item.id">
                <span class="badge">评论人：{{ item.name }}</span>
                <span>{{ item.comment }}</span>
            </li>

        </ul>

    </div>

    <script>
        Vue.component("comment", {
            template: "#comment",
            data() {
                return {
                    name: "",
                    comment: "",
                }
            },
            methods: {
                add() {
                    if (this.name === "" || this.comment === "") return
                    this.$emit("com", this.name, this.comment)
                    this.name = this.comment = ""
                }
            }
        })


        var vm = new Vue({
            el: '#app',
            data: {
                list: [
                    { id: Date.now().toString(), name: "李白", comment: "床前明月光" },
                    { id: Date.now().toString(), name: "王伟", comment: "哈哈哈，好有才" },
                    { id: Date.now().toString(), name: "杜甫", comment: "我是第一名" },
                    { id: Date.now().toString(), name: "王五", comment: "砍刀" }
                ]
            },
            methods: {
                myadd(name, comment) {
                    console.log(name, comment)
                    this.list.unshift({ id: Date.now.toString(), name: name, comment: comment })
                }
            },
        })
    </script>
</body>

</html>